package com.jumptap.adtag.utils;

import android.content.Context;
import android.util.Log;
import com.jumptap.adtag.listeners.JtAdViewInnerListener;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class JtAdFetcher
  implements Runnable
{
  private static final String ADVIEW_HTML_FILE_NAME = "adview.html";
  private static final String ADVIEW_JS_FILE_NAME = "adview.js";
  private static final String AD_CONTENT_HERE_STR = "%AD_CONTENT_HERE%";
  private static final int BUFFER_SIZE = 1024;
  private static final String JS_CONTENT_HERE_STR = "%JS_CONTENT_HERE%";
  private static final String XJT_RESPONSE_ID = "Xjt-Responseid";
  private String adRequestId;
  private JtAdViewInnerListener contentListener = null;
  private Context context;
  private boolean shouldDebugNetworkTraffic = false;
  private String url;
  private String wrapperContent = null;

  public JtAdFetcher(Context paramContext, JtAdViewInnerListener paramJtAdViewInnerListener)
    throws JtException
  {
    this.contentListener = paramJtAdViewInnerListener;
    this.context = paramContext;
    makeContentWrapper();
  }

  private void makeContentWrapper()
    throws JtException
  {
    if (this.wrapperContent == null)
    {
      StringBuilder localStringBuilder1 = FileReaderUtil.getFileContent(this.context, "adview.html");
      StringBuilder localStringBuilder2 = FileReaderUtil.getFileContent(this.context, "adview.js");
      this.wrapperContent = localStringBuilder1.toString().replace("%JS_CONTENT_HERE%", localStringBuilder2);
    }
  }

  private String modifyHtml(String paramString)
  {
    String str = null;
    if (paramString != null)
    {
      if (paramString.length() > 0)
        str = this.wrapperContent.replace("%AD_CONTENT_HERE%", paramString);
    }
    else
      return str;
    return "";
  }

  public String getAdContent()
  {
    DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient();
    HttpGet localHttpGet = new HttpGet(this.url);
    if (this.shouldDebugNetworkTraffic)
      Log.d("JtAd", "Created html client for: " + this.url);
    int i;
    StringBuilder localStringBuilder;
    try
    {
      HttpResponse localHttpResponse = localDefaultHttpClient.execute(localHttpGet);
      Header localHeader = localHttpResponse.getFirstHeader("Xjt-Responseid");
      if (localHeader != null)
        this.adRequestId = localHeader.getValue();
      i = localHttpResponse.getStatusLine().getStatusCode();
      if (i != 200)
        break label235;
      HttpEntity localHttpEntity = localHttpResponse.getEntity();
      ((int)localHttpEntity.getContentLength());
      InputStream localInputStream = localHttpEntity.getContent();
      byte[] arrayOfByte = new byte[1024];
      localStringBuilder = new StringBuilder();
      while (true)
      {
        int j = localInputStream.read(arrayOfByte);
        if (j <= 0)
          break;
        localStringBuilder.append(new StringBuffer(new String(arrayOfByte, 0, j)));
      }
    }
    catch (IOException localIOException)
    {
      Log.e("JtAd", "JtAdFetcher.getAdContent:" + localIOException.toString());
      return null;
    }
    return localStringBuilder.toString();
    label235: Log.e("JtAd", "JtAdFetcher.getAdContent: Recieve error Code=[" + i + "] when sending url=[" + this.url + "]");
    return null;
  }

  public String getAdRequestId()
  {
    return this.adRequestId;
  }

  public void kickOffUrlFetch(String paramString)
  {
    Log.d("JtAd", "Kicked off fetcher");
    this.url = paramString;
    new Thread(this).start();
  }

  public void run()
  {
    Log.d("JtAd", "Started Url Thread");
    String str = modifyHtml(getAdContent());
    if (this.contentListener != null)
      this.contentListener.setContent(str, this.adRequestId);
  }

  public void setShouldDebugNetworkTraffic(boolean paramBoolean)
  {
    this.shouldDebugNetworkTraffic = paramBoolean;
  }

  public void setUrl(String paramString)
  {
    this.url = paramString;
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.jumptap.adtag.utils.JtAdFetcher
 * JD-Core Version:    0.6.2
 */